<?php
/**
 *
 * @author: xaboy<365615158@qq.com>
 * @day: 2017/11/02
 */
namespace basic;

use think\Db;
use think\Model;
use think\Session;
use think\Cache;

use think\Config;

class ModelBasic extends Model
{
	const LIMIT = 15;
	const DEFAULT_SORT_RULE = 'ASC';

    public static $softDelete = false;

    private static $errorMsg;

    private static $transaction = 0;

    private static $DbInstance = [];

    /**
     * 布尔键值
     * @var [type]
     */
    public static $boolean = [
        0 => '否',
        1 => '是'
    ];

    const STATUS_VALID = 1;

    public static $common_status = [
        1 => '启用',
        0 => '禁用'
    ];

    const DEFAULT_ERROR_MSG = '操作失败,请稍候再试!';

    protected static function getDb($name,$update = false)
    {
        if(isset(self::$DbInstance[$name]) && $update == false)
            return self::$DbInstance[$name];
        else
            return self::$DbInstance[$name] = Db::name($name);
    }

    /**
     * 设置错误信息
     * @param string $errorMsg
     * @return bool
     */
    protected static function setErrorInfo($errorMsg = self::DEFAULT_ERROR_MSG,$rollback = false)
    {
        if($rollback) self::rollbackTrans();
        self::$errorMsg = $errorMsg;
        return false;
    }

    /**
     * 获取错误信息
     * @param string $defaultMsg
     * @return string
     */
    public static function getErrorInfo($defaultMsg = self::DEFAULT_ERROR_MSG)
    {
        return !empty(self::$errorMsg) ? self::$errorMsg : $defaultMsg;
    }

    /**
     * 开启事务
     */
    public static function beginTrans()
    {
        Db::startTrans();
    }

    /**
     * 提交事务
     */
    public static function commitTrans()
    {
        Db::commit();
    }

    /**
     * 关闭事务
     */
    public static function rollbackTrans()
    {
        Db::rollback();
    }

    /**
     * 根据结果提交滚回事务
     * @param $res
     */
    public static function checkTrans($res)
    {
        if($res){
            self::commitTrans();
        }else{
            self::rollbackTrans();
        }
    }

    /**
     * 处理列表数据
     * @Author   FW
     * @DateTime 2021-01-12
     * @Email    123763712@qq.com
     * @param    array            $list [description]
     * @return   [type]                 [description]
     */
    public static function process_list (array $list)
    {
        return $list;
    }

    /**
     * 列表
     * @Author   FW
     * @DateTime 2020-09-24
     * @Email    123763712@qq.com
     * @param    [type]
     * @return   [type]
     */
    public static function getList($where){
        $count = static::setWhere($where)->count();
        $data  = [];
        if ( $count == 0 ) {
            return compact('data','count');
        }

        $data = static::setWhere($where,'m')
        ->field('m.*')
        ->page($where['page'] ?? 1,$where['limit'] ?? 20)
        ->select();

        if ( !empty($data) ) {
            $data = $data->toArray();

            $data = static::process_list($data);
        }

        return compact('data','count');
    }

    /**
     * 默认条件设置
     * @Author   FW
     * @DateTime 2021-08-10
     * @Email    123763712@qq.com
     * @param    [type]           $where [description]
     */
    public static function setWhere($where)
    {
        return static::alias('m');
    }

    /**
     * 获取数组形式全部list
     * @Author   FW
     * @DateTime 2021-04-07
     * @Email    123763712@qq.com
     * @param    array            $params [description]
     * @return   [type]                   [description]
     */
    public static function getListArr ($params = [] )
    {
        $model = static::setWhere($params);
        $obj   = $model->select();

        if ( !empty($obj) ) {
            $data = $obj->toArray();

            $data = static::process_list($data);
            return $data;
        }
        return [];
    }

    /**
     * [getOne description]
     * @Author   FW
     * @DateTime 2021-01-12
     * @Email    123763712@qq.com
     * @param    [type]           $id [description]
     * @return   [type]               [description]
     */
    public static function getOne ($id)
    {
        $item = self::alias('m')->where('m.id',$id)->find();
        return $item;
    }

    /**
     * 删除
     * @Author   FW
     * @DateTime 2021-01-12
     * @Email    123763712@qq.com
     * @param    [type]           $id [description]
     * @return   [type]               [description]
     */
    public static function deleteOne ($id)
    {
        $model = self::where('id' , $id);

        if ( static::$softDelete ) {
            return $model->update(['is_del' => time()]);
        }

        return $model->delete();
    }

    /**
     * 保存后回调
     * @Author   FW
     * @DateTime 2021-05-10
     * @Email    123763712@qq.com
     * @param    [type]           $prk_id [description]
     * @param    array            $data   [description]
     * @return   [type]                   [description]
     */
    public static function afterSave ( $prk_id , array $data )
    {
        return true;
    }

}